package cn.iocoder.yudao.module.system.controller.admin.tenant;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.institution.InstitutionPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.institution.InstitutionRespVO;
import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.institution.InstitutionSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.InstitutionDO;
import cn.iocoder.yudao.module.system.service.tenant.InstitutionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 机构管理")
@RestController
@RequestMapping("/institution/")
@Validated
public class InstitutionController {

    @Resource
    private InstitutionService institutionService;

    @PostMapping("/create")
    @Operation(summary = "创建机构管理")
    //@PreAuthorize("@ss.hasPermission('institution::create')")
    public CommonResult<Long> create(@Valid @RequestBody InstitutionSaveReqVO createReqVO) {
        return success(institutionService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机构管理")
    //@PreAuthorize("@ss.hasPermission('institution::update')")
    public CommonResult<Boolean> update(@Valid @RequestBody InstitutionSaveReqVO updateReqVO) {
        institutionService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机构管理")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('institution::delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        institutionService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机构管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('institution::query')")
    public CommonResult<InstitutionRespVO> get(@RequestParam("id") Long id) {
        InstitutionDO institutionDO = institutionService.get(id);
        return success(BeanUtils.toBean(institutionDO, InstitutionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机构管理分页")
    //@PreAuthorize("@ss.hasPermission('institution::query')")
    public CommonResult<PageResult<InstitutionRespVO>> getPage(@Valid InstitutionPageReqVO pageReqVO) {
        PageResult<InstitutionDO> pageResult = institutionService.getPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InstitutionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机构管理 Excel")
    //@PreAuthorize("@ss.hasPermission('institution::export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportExcel(@Valid InstitutionPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InstitutionDO> list = institutionService.getPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机构管理.xls", "数据", InstitutionRespVO.class, BeanUtils.toBean(list, InstitutionRespVO.class));
    }

}